package chances.epg.search.analyzer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.slf4j.Logger;


import chances.epg.commons.log.EpgLogFactory;


public class SplitAnalyzerUtils {
	private static final Logger logger = EpgLogFactory.getSearchLogger();
	private static SplitAnalyzerUtils instance;
	
	private Analyzer analyzer;
	
	protected SplitAnalyzerUtils() {
		analyzer = new SplitAnalyzer();
	}
	
	public static SplitAnalyzerUtils getInstance() {
		if (instance == null) {
			instance = new SplitAnalyzerUtils();
		}
		
		return instance;
	}
	public String[] split(String source) {
		List<String> result = new ArrayList<String>();
		try {
			TokenStream tokenStream = analyzer.tokenStream("split", source);
			CharTermAttribute termAtt = tokenStream.getAttribute(CharTermAttribute.class);
			tokenStream.reset();
			while (tokenStream.incrementToken()) {
				if(!result.contains(termAtt.toString())){
				    result.add(termAtt.toString());
				}
	        }
			tokenStream.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		return result.toArray(new String[0]);
	}
	

}
